package cz.semjob.learning.instances.loader;

import java.io.FileReader;
import java.io.IOException;

import org.springframework.stereotype.Component;

import weka.core.Instance;
import weka.core.Instances;

@Component
public class ARFFDataSetLoader {

	public Instances loadDataSet(String filename) throws IOException {
		return loadDataSet(filename, 1d, 1d);
	}

	public Instances loadDataSet(String filename, double positiveWeight,
			double negativeWeight) throws IOException {
		FileReader reader = new FileReader(filename);
		Instances instances = new Instances(reader);

		// instances.
		int attrNum = instances.numAttributes() - 1;
		for (int i = 0; i < instances.numInstances(); i++) {
			Instance instance = instances.instance(i);
			if (instance.value(attrNum) == 0d) {
				instance.setWeight(1);
			} else {
				instance.setWeight(0.5);
			}
		}
		// Make the last attribute be the class
		instances.setClassIndex(attrNum);

		return instances;
	}
}
